package com.liuxinlong.jobs;

import com.liuxinlong.api.ThreeColorInterface;
import com.liuxinlong.modules.dao.DataPushDao;
import com.liuxinlong.modules.entity.DataPushRecord;
import com.liuxinlong.utils.ObjectUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 使用quartz框架
 */
@Service
@Slf4j
@Component("pushIncrementalDataTask")
public class PushIncrementalDataTask {

    @Autowired
    private DataPushDao dataPushDao;

    @Autowired
    private ThreeColorInterface threeColorInterface;


    /**
     * 业务逻辑
     */
    public void reptilian() {
        log.info("PushIncrementalDataTask start...");
        List<DataPushRecord> pendingRecordList = dataPushDao.getPendingList();
        List<Map<String, String>> incrementalFileList = new ArrayList<>(pendingRecordList.size());
        for (DataPushRecord dataPushRecord : pendingRecordList) {
            Map<String, String> item = new HashMap<>();
            item.put("pdf_id", dataPushRecord.getContentOne());
            item.put("pdf_name", dataPushRecord.getContentTwo());
            incrementalFileList.add(item);
        }
        if (!ObjectUtils.isEmpty(incrementalFileList)) {
            threeColorInterface.pushIncrementalFileData(incrementalFileList);
            List<String> targetList = pendingRecordList.stream().map(DataPushRecord::getId).collect(Collectors.toList());
            dataPushDao.updateDataPushRecordStatus(targetList, 2);
        } else {
            log.info("no incremental data need push...");
        }

        log.info("PushIncrementalDataTask end...");
    }
}

